package io.renren.modules.aic.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.DecimalUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.aic.entity.MachineEntity;
import io.renren.modules.aic.service.MachineService;

import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 矿机表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-03-30 15:44:17
 */
@RestController
@RequestMapping("aic/machine")
public class MachineController {
	@Autowired
	private MachineService machineService;

	/**
	 * 上架中的矿机
	 */
	@RequestMapping("/types")
	public R types() {
		return R.ok().put("machineType", machineService.selectList(new EntityWrapper<MachineEntity>().eq("status", ProjectConstant.MACHINE_STATUS_ENABLE)));
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("aic:machine:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = machineService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("aic:machine:info")
	public R info(@PathVariable("id") Long id) {
		MachineEntity machine = machineService.selectById(id);

		return R.ok().put("machine", machine);
	}

	/**
	 * 保存
	 */
	@SysLog("添加系统矿机")
	@RequestMapping("/save")
	@RequiresPermissions("aic:machine:save")
	public R save(@RequestBody MachineEntity machine) {
		// 校验类型
		ValidatorUtils.validateEntity(machine);
		Assert.isTrue(machine.getDayProfit() > 0, "每日收益必须大于0");
		machine.setTotalProfit(DecimalUtils.mul(machine.getDayProfit(), machine.getTotalDays()));
		machine.setCreateTime(new Date());
		machineService.insert(machine);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改系统矿机")
	@RequestMapping("/update")
	@RequiresPermissions("aic:machine:update")
	public R update(@RequestBody MachineEntity machine) {
		// 校验类型
		ValidatorUtils.validateEntity(machine);
		Assert.isTrue(machine.getDayProfit() > 0, "每日收益必须大于0");
		machine.setTotalProfit(DecimalUtils.mul(machine.getDayProfit(), machine.getTotalDays()));
		machine.setUpdateTime(new Date());
		machineService.updateById(machine);

		return R.ok();
	}

	/**
	 * 上架/下架
	 */
	@SysLog("矿机上架/下架")
	@RequestMapping("/changeStatus")
	@RequiresPermissions("aic:machine:update")
	public R changeStatus(Integer status, Long[] machineIds) {
		Assert.notNull(status, "参数异常[status]");
		Assert.notNull(machineIds, "machineIds is null");
		Assert.noNullElements(machineIds, "参数异常[machineIds]");
		MachineEntity entity = new MachineEntity();
		entity.setStatus(status);
		machineService.update(entity, new EntityWrapper<MachineEntity>().in("id", machineIds));

		return R.ok();
	}

}
